<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        x: {{ position.x }} <br>
        y: {{ position.y }}
    </div>
    <script type="module">
        import { createApp, reactive, onMounted, onUnmounted } from './node_modules/vue/dist/vue.esm-browser.js'
        // 鼠标移动函数的封装
        function mouseMove() {
            const position = reactive({
                x: 0,
                y: 0
            })
            // 鼠标移动函数封装
            const update = e => {
                position.x = e.pageX
                position.y = e.pageY
            }
            // 钩子函数需要接收一个参数且该参数也是一个函数
            onMounted(() => {
                window.addEventListener('mousemove', update)
            }),
            // 同上
            onUnmounted(() => {
                window.removeEventListener('mousemove', update)
            })
            // 将动态的鼠标移动数据返回,使得组件中可以动态渲染
            return position
        }
        // 创建vue对象
        const app = createApp({
            setup(props, context) {
                // 调用鼠标移动函数拿到鼠标移动的数据,然后return出去
                const position = mouseMove()
                return {
                    position
                }
            }
        })
        app.mount('#app')
    </script>
</body>
</html>